package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.Brand;
import com.zryc.shop.pms.entity.Product;
import com.zryc.shop.pms.entity.ProductCategory;
import com.zryc.shop.pms.service.IBrandService;
import com.zryc.shop.pms.service.IProductCategoryService;
import com.zryc.shop.pms.service.IProductService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/product")
@Api(tags = "商品管理接口列表")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IProductCategoryService categoryService;
    @Autowired
    private IBrandService brandService;

    @GetMapping("/simpleList")
    @ApiOperation("查询商品简略信息列表")
    public ResponseResult simpleList(@RequestParam @ApiParam(value = "商品名称/货号关键词") String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            return new ResponseResult(400, "关键字不能为空");
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("id", "name", "product_sn");
            queryWrapper.like("name", keyword);
            queryWrapper.or();
            queryWrapper.eq("product_sn", keyword);
            return new ResponseResult(200, "查询完成", productService.list(queryWrapper));
        }
    }

    @PostMapping("/create")
    @ApiOperation("添加商品")
    @Transactional
    public ResponseResult create(@RequestBody Product product) {
//        product.setPromotionStartTime(product.getPromotionStartTime().plusHours(8));
//        product.setPromotionEndTime(product.getPromotionEndTime().plusHours(8));
        productService.save(product);
        //1.先试用类别ID查找对应的商品数量
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("product_count");
        queryWrapper.eq("id", product.getProductCategoryId());
        ProductCategory category = categoryService.getOne(queryWrapper);
        //2.商品数量+1后更新
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", product.getProductCategoryId());
        updateWrapper.set("product_count", category.getProductCount() + 1);
        categoryService.update(updateWrapper);
        //修改品牌对应的商品数量同上
        queryWrapper = new QueryWrapper();
        queryWrapper.select("product_count");
        queryWrapper.eq("id", product.getBrandId());
        Brand brand = brandService.getOne(queryWrapper);

        updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", product.getBrandId());
        updateWrapper.set("product_count", brand.getProductCount() + 1);
        brandService.update(updateWrapper);

        return new ResponseResult(200, "添加成功");
    }

    @GetMapping("/updateInfo/{id}")
    @ApiOperation("查询商品详情")
    public ResponseResult updateInfo(@PathVariable Long id) {
        return new ResponseResult(200, "查询完成", productService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改商品信息")
    public ResponseResult update(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
//        product.setPromotionStartTime(product.getPromotionStartTime().plusHours(8));
//        product.setPromotionEndTime(product.getPromotionEndTime().plusHours(8));
        productService.updateById(product);
        return new ResponseResult(200, "修改成功");
    }

    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam("商品名称关键词") String keyword,
                               @RequestParam(required = false) @ApiParam("商品货号") String productSn,
                               @RequestParam(required = false) @ApiParam("品牌ID") Integer brandId,
                               @RequestParam(required = false) @ApiParam("商品类别ID") Integer productCategoryId,
                               @RequestParam(required = false) @ApiParam("上/下架状态") Integer publishStatus,
                               @RequestParam(required = false) @ApiParam("审核状态") Integer verifyStatus,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("name", keyword);
        }
        if (!StringUtils.isEmpty(productSn)) {
            queryWrapper.eq("product_sn", productSn);
        }
        if (brandId!=null) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (productCategoryId!=null) {
            queryWrapper.eq("product_category_id", productCategoryId);
        }
        if (publishStatus!=null) {
            queryWrapper.eq("publish_status", publishStatus);
        }
        if (verifyStatus!=null) {
            queryWrapper.eq("verify_status", verifyStatus);
        }
        productService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/update/deleteStatus")
    @ApiOperation("修改删除状态")
    public ResponseResult delete(String ids, Integer deleteStatus) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("delete_status", deleteStatus);
        productService.update(updateWrapper);
        return new ResponseResult(200, deleteStatus==1?"删除成功":"找回成功");
    }

    @PostMapping("/update/newStatus")
    @ApiOperation("设置/取消新品")
    public ResponseResult updateNewStatus(String ids, Integer newStatus) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("new_status", newStatus);
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/recommendStatus")
    @ApiOperation("修改推荐状态")
    public ResponseResult updateRecommendStatus(String ids, Integer recommendStatus) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("recommand_status", recommendStatus);
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/publishStatus")
    @ApiOperation("修改上架状态")
    public ResponseResult updatePublishStatus(String ids, Integer publishStatus) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("publish_status", publishStatus);
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }



}
